// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成

package com.fowo.api.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.common.excel.TemplateModelBuildEventListener;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.RException;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.common.util.SheetUtils;
import com.fowo.api.entity.Receivable;
import com.fowo.api.mapper.ReceivableMapper;
import com.fowo.api.mapper.SupplierMapper;
import com.fowo.api.model.receivable.*;
import com.fowo.api.model.receivable.constant.ReceivablePushStatusEnums;
import com.fowo.api.model.receivable.product.ReceivableProductSearchParamPo;
import com.fowo.api.model.supplier.SupplierItemVo;
import com.fowo.api.model.supplier.SupplierSearchParamPo;
import com.fowo.api.service.ReceivableProductService;
import com.fowo.api.service.ReceivableService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.sys.entity.SysImportTemplate;
import com.fowo.api.sys.mapper.SysDepartmentMapper;
import com.fowo.api.sys.model.NewSerialRequest;
import com.fowo.api.sys.model.SysDepartmentSearchParamPo;
import com.fowo.api.sys.model.SysDepartmentVo;
import com.fowo.api.sys.model.SysDictAllItem;
import com.fowo.api.sys.service.FileService;
import com.fowo.api.sys.service.SysDictService;
import com.fowo.api.sys.service.SysImportTemplateService;
import com.fowo.api.sys.service.SysSerialService;
import com.fowo.api.user.model.JwtUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.io.InputStream;
import java.util.*;

/**
 * 应收单 服务实现类
 */
@Service
@Slf4j
@DS("ds24")
public class ReceivableServiceImpl
  extends ReceivableBaseServiceImpl {

  /**
   * 更新所有子表
   * @param model 应收单
   */
  protected void updateAllChilds(ReceivableVo model) throws Exception {
    if (model.getProducts() != null) {
      receivableProductService.saveAllByParentId(
              model.getId(),
              model.getProducts()
      );
    }
  }

  /**
   * 更新应收单
   * @param model 应收单
   * @return 更新是否成功
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean update(ReceivableVo model) throws Exception {
    JwtUserInfo currentUser = JwtUserInfo.fromHeader();
    Receivable entity = this.getById(model.getId());
    Assert.isTrue(entity.getStatus().equals("0"),"已审核不能修改");
    if (entity == null) {
      throw new Exception("应收单不存在");
    }
    BeanUtils.copyProperties(
            model,
            entity,
            "id",
            "payCode",
            "createTime",
            "createUser",
            "lastUpdateTime",
            "lastUpdateUser"
    );
    entity.setLastUpdateTime(new Date());
    if (currentUser != null) {
      entity.setLastUpdateUser(currentUser.getUserId());
    }
    if (this.updateById(entity)) {
      updateAllChilds(model);
      baseMapper.updateCalcFields(entity.getId());
      return true;
    }
    return false;
  }

  /**
   * 更新应收单（带空值）
   * @param model 应收单
   * @return 更新是否成功
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean updateForEdit(ReceivableVo model) throws Exception {
    JwtUserInfo currentUser = JwtUserInfo.fromHeader();
    Receivable entity = this.getById(model.getId());
    Assert.isTrue(entity.getStatus().equals("0"),"已审核不能修改");
    if (entity == null) {
      throw new Exception("应收单不存在");
    }
    model.setLastUpdateTime(new Date());
    if (currentUser != null) {
      model.setLastUpdateUser(currentUser.getUserId());
    }
    boolean isOk = new LambdaUpdateChainWrapper<>(baseMapper)
            .set(Receivable::getLastUpdateTime, model.getLastUpdateTime())
            .set(Receivable::getLastUpdateUser, model.getLastUpdateUser())
            .set(Receivable::getCustomer, model.getCustomer())
            .set(Receivable::getCloseOrg, model.getCloseOrg())
            .set(Receivable::getBusinessDate, model.getBusinessDate())
            .set(Receivable::getRemark, model.getRemark())
            .eq(Receivable::getId, model.getId())
            .update();
    if (isOk) {
      updateAllChilds(model);
      return true;
    }
    return false;
  }
  /**
   * 删除应收单
   * @param id 应收单的主键
   * @return 删除是否成功
   */
  @Override
  public boolean delete(Long id) throws Exception {
    Receivable entity = this.getById(id);
    Assert.isTrue(entity.getStatus().equals("0"),"已审核不能删除");
    return this.removeById(id);
  }


}
